/*
* CleanService.java
* Created on  202015/6/15 17:08
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/6/15       longshihui    初始版本
*
*/

package com.infitecs.eyas.combustion.service;

import com.infitecs.eyas.boiler.dao.AirGasParameterDao;
import com.infitecs.eyas.boiler.dao.EfficiencyDao;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.combustion.dao.CleanDao;
import com.infitecs.eyas.combustion.model.CalculateClean;
import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.expression.service.ExpressionService;
import com.infitecs.eyas.nature.dao.NatureDao;
import com.infitecs.eyas.nature.model.Nature;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.quality.dao.AshDao;
import com.infitecs.eyas.quality.dao.ElementDao;
import com.infitecs.eyas.quality.dao.IndustryDao;
import com.infitecs.eyas.quality.model.Ash;
import com.infitecs.eyas.quality.model.Element;
import com.infitecs.eyas.quality.model.Industry;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.session.model.Param;
import com.infitecs.eyas.step.modal.StepInfo;
import com.infitecs.eyas.util.DoubleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 类的描述信息
 *
 * @author longshihui
 * @version 1.0.1
 */
@Service
@Transactional
public class CleanService {
    @Autowired
    private CleanDao cleanDao;
    @Autowired
    private SessionService sessionService;
    @Autowired
    private BurnService burnService;
    @Autowired
    private ExpressionService expressionService;
    @Autowired
    private OperateFieldDao operateFieldDao;

    public CalculateClean getCalculateClean() {
        return cleanDao.getCalculateClean(sessionService.getCaseId());
    }

    public CalculateClean getCalculateClean(int caseId) {
        return cleanDao.getCalculateClean(caseId);
    }

    public void addCalculateClean() {
        int caseId = sessionService.getCaseId();
        sessionService.addStepState(new StepInfo(caseId, Constant.STEP_ID_8, Constant.STEP_OVER_STATE_0));
        cleanDao.addCalculateClean(caseId);
    }

    public void saveValueOfClean(CalculateClean calculateClean) {
        calculateClean.setCaseId(sessionService.getCaseId());
        cleanDao.saveValueOfClean(calculateClean);
        calculateClean.setParamName(sessionService.getE() + calculateClean.getParamName());
        sessionService.putParamMap(calculateClean);
    }

    public void saveVo(CalculateClean calculateClean) {
        calculateClean.setCaseId(sessionService.getCaseId());
        cleanDao.saveVo(calculateClean);
    }

    public void cleanSave(CalculateClean calculateClean) {
        cleanDataSave(calculateClean);
    }

    public Map<String, Double> getCleanDataByCaseId(int caseId) {
        CalculateClean calculateClean = cleanDao.getCalculateClean(caseId);
        OperateField operateField = operateFieldDao.getOperateField(caseId);
        calculateClean.setCaseId(operateField.getCaseId());
        calculateClean.setE221State(operateField.getE221State());
        calculateClean.setE222State(operateField.getE222State());
        calculateClean.setE231State(operateField.getE231State());
        calculateClean.setE241State(operateField.getE241State());
        calculateClean.setE242State(operateField.getE242State());
        calculateClean.setE248State(operateField.getE248State());
        calculateClean.setE258State(operateField.getE258State());
        return getCleanData(calculateClean);
    }

    public void cleanDataSave(CalculateClean calculateClean) {
        Map<String, Double> dataMap = getCleanData(calculateClean);
        calculateClean.setE221(dataMap.get("E221"));
        calculateClean.setE222(dataMap.get("E222"));
        calculateClean.setE231(dataMap.get("E231"));
        calculateClean.setE241(dataMap.get("E241"));
        calculateClean.setE242(dataMap.get("E242"));
        calculateClean.setE248(dataMap.get("E248"));
        calculateClean.setE258(dataMap.get("E258"));
        calculateClean.setE244(dataMap.get("E244"));
        calculateClean.setE331(dataMap.get("E331"));
        calculateClean.setE253(dataMap.get("E253"));
        calculateClean.setE254(dataMap.get("E254"));
        calculateClean.setE255(dataMap.get("E255"));
        calculateClean.setE262(dataMap.get("E262"));
        calculateClean.setE263(dataMap.get("E263"));
        calculateClean.setE264(dataMap.get("E264"));
        calculateClean.setE285(dataMap.get("E285"));

        CalculateClean oldData = cleanDao.getCalculateClean(calculateClean.getCaseId());
        if (oldData == null) {
            cleanDao.addCalculateCleanData(calculateClean);
        } else {
            cleanDao.saveVo(calculateClean);
        }
        OperateField operateField = new OperateField();
        operateField.setCaseId(calculateClean.getCaseId());
        operateField.setE221State(calculateClean.getE221State());
        operateField.setE222State(calculateClean.getE222State());
        operateField.setE231State(calculateClean.getE231State());
        operateField.setE241State(calculateClean.getE241State());
        operateField.setE242State(calculateClean.getE242State());
        operateField.setE248State(calculateClean.getE248State());
        operateField.setE258State(calculateClean.getE258State());
        operateFieldDao.updateOperateFieldClean(operateField);
    }

    public Map<String, Double> getCleanData(CalculateClean calculateClean) {
        Map<String, Formula> formulaMap = expressionService.getFormulaMapByType(6);
        Map<String, Double> paramMap = burnService.getBurnDataByCaseId(calculateClean.getCaseId());
        //221
        if (calculateClean.getE221State().equals("1")) {
            paramMap.put("E221", calculateClean.getE221());
        }
        //222
        if (calculateClean.getE222State().equals("1")) {
            paramMap.put("E222", calculateClean.getE222());
        }
        paramMap.put("E223", calculateClean.getE223());
        paramMap.put("E224", calculateClean.getE224());
        //E225
        double E225 = expressionService.getCalculateData(formulaMap.get("RE225"), paramMap);
        paramMap.put("E225", E225);
        //E226
        double E226 = expressionService.getCalculateData(formulaMap.get("R226"), paramMap);
        paramMap.put("E226", E226);
        paramMap.put("E227", calculateClean.getE227());
        paramMap.put("E228", calculateClean.getE228());
        paramMap.put("E229", calculateClean.getE229());
        paramMap.put("E230", calculateClean.getE230());
        //E231
        if (calculateClean.getE231State().equals("1")) {
            paramMap.put("E231", calculateClean.getE231());
        } else {
            double E231 = expressionService.getCalculateData(formulaMap.get("RE231"), paramMap);
            paramMap.put("E231", E231);
        }
        //E232
        double E232 = expressionService.getCalculateData(formulaMap.get("RE232"), paramMap);
        paramMap.put("E232", E232);
        //E233
        double E233 = expressionService.getCalculateData(formulaMap.get("RE233"), paramMap);
        paramMap.put("E233", E233);
        //E234
        double E234 = paramMap.get("E231") < 40 ? 0.03906 * paramMap.get("E231") - 0.1444 : 0.0202 * paramMap.get("E231") + 0.45184;
        paramMap.put("E234", DoubleUtil.getInstance().get4Double(E234));
        //E235
        double E235 = expressionService.getCalculateData(formulaMap.get("RE235"), paramMap);
        paramMap.put("E235", E235);
        //E236
        double E236 = expressionService.getCalculateData(formulaMap.get("RE236"), paramMap);
        paramMap.put("E236", E236);
        //E237
        double E237 = expressionService.getCalculateData(formulaMap.get("RE237"), paramMap);
        paramMap.put("E237", E237);
        //E238
        double E238 = expressionService.getCalculateData(formulaMap.get("RE238"), paramMap);
        paramMap.put("E238", E238);
        //E239
        double E239 = expressionService.getCalculateData(formulaMap.get("RE239"), paramMap);
        paramMap.put("E239", E239);
        //E240
        double E240 = expressionService.getCalculateData(formulaMap.get("RE240"), paramMap);
        paramMap.put("E240", E240);
        //E241
        double GE129 = expressionService.getCalculateData(formulaMap.get("RE241_1"), paramMap);
        if (calculateClean.getE241State().equals("1")) {
            paramMap.put("E241", calculateClean.getE241());
        } else {
            Double RE241_1 = 30 <= GE129 && GE129 < 45 ? 415.0 : 300.0;
            Double RE241_2 = 20 <= GE129 && GE129 < 30 ? 440 : RE241_1;
            Double RE241_3 = 1 <= GE129 && GE129 < 20 ? 475 : RE241_2;
            Double RE241_4 = 0.05 <= GE129 && GE129 < 1 ? 556 : RE241_3;
            double E241 = 0 <= GE129 && GE129 < 0.05 ? 800 : RE241_4;
            paramMap.put("E241", E241);
        }
        //E242
        if (calculateClean.getE242State().equals("1")) {
            paramMap.put("E242", calculateClean.getE242());
        } else {
            double E242_1 = expressionService.getCalculateData(formulaMap.get("RE242_1"), paramMap);
            double E242_2 = expressionService.getCalculateData(formulaMap.get("RE242_2"), paramMap);
            double E242 = paramMap.get("ME36") < 15 ? E242_1 : E242_2;
            paramMap.put("E242", E242);
        }
        //E243
        double E243 = expressionService.getCalculateData(formulaMap.get("RE243"), paramMap);
        paramMap.put("E243", E243);
        //E244
        double E244 = expressionService.getCalculateData(formulaMap.get("RE244"), paramMap);
        paramMap.put("E244", E244);
        //246
        paramMap.put("E246", calculateClean.getE246());
        //E247
        double E247 = expressionService.getCalculateData(formulaMap.get("RE247"), paramMap);
        paramMap.put("E247", E247);
        //E248
        if (calculateClean.getE248State().equals("1")) {
            paramMap.put("E248", calculateClean.getE248());
        } else {
            double E248 = expressionService.getCalculateData(formulaMap.get("RE248"), paramMap);
            paramMap.put("E248", E248);
        }
        //E249
        double E249 = expressionService.getCalculateData(formulaMap.get("RE249"), paramMap);
        paramMap.put("E249", E249);
        //E250
        double E250 = expressionService.getCalculateData(formulaMap.get("RE250"), paramMap);
        paramMap.put("E250", E250);
        //E251
        double E251 = expressionService.getCalculateData(formulaMap.get("RE251"), paramMap);
        paramMap.put("E251", E251);
        //E252
        double E252 = expressionService.getCalculateData(formulaMap.get("RE252"), paramMap);
        paramMap.put("E252", E252);
        //E253
        double E253 = expressionService.getCalculateData(formulaMap.get("RE253"), paramMap);
        paramMap.put("E253", E253);
        //E254
        double E254 = expressionService.getCalculateData(formulaMap.get("RE254"), paramMap);
        paramMap.put("E254", E254);
        //255
        double RE255_1 = expressionService.getCalculateData(formulaMap.get("RE255_1"), paramMap);
        paramMap.put("E255", paramMap.get("ME35") < RE255_1 ? 1.0 : -1.0);
        //E257
        paramMap.put("E257", calculateClean.getE257());
        //E258
        if (calculateClean.getE248State().equals("1")) {
            paramMap.put("E258", calculateClean.getE258());
        } else {
            double E258 = expressionService.getCalculateData(formulaMap.get("RE258"), paramMap);
            paramMap.put("E258", E258);
        }
        //E259
        double E259 = expressionService.getCalculateData(formulaMap.get("RE259"), paramMap);
        paramMap.put("E259", E259);
        //E260
        Double RE260_2;
        Double ET1260 = expressionService.getCalculateData(formulaMap.get("RET1260"), paramMap);
        paramMap.put("ET1260", ET1260);
        if (1 - ET1260 > 0) {
            RE260_2 = expressionService.getCalculateData(formulaMap.get("RE260_2"), paramMap);
        } else {
            RE260_2 = expressionService.getCalculateData(formulaMap.get("RE260_4"), paramMap);
            RE260_2=0-RE260_2;
        }
        Double RE260_1 = expressionService.getCalculateData(formulaMap.get("RE260_1"), paramMap);
        Double RE260_3 = expressionService.getCalculateData(formulaMap.get("RE260_3"), paramMap);
        paramMap.put("E260", DoubleUtil.getInstance().get4Double((RE260_1 + RE260_2) * RE260_3));
        //E261
        double E261 = expressionService.getCalculateData(formulaMap.get("RE261"), paramMap);
        paramMap.put("E261", E261);
        //E262
        double E262 = expressionService.getCalculateData(formulaMap.get("RE262"), paramMap);
        paramMap.put("E262", E262);
        //E263
        double E263 = expressionService.getCalculateData(formulaMap.get("RE263"), paramMap);
        paramMap.put("E263", E263);
        //E264
        double E264 = expressionService.getCalculateData(formulaMap.get("RE264"), paramMap);
        paramMap.put("E264", E264);
        //E265
        double E265 = expressionService.getCalculateData(formulaMap.get("RE265"), paramMap);
        paramMap.put("E265", E265);
        //E266
        double E266 = expressionService.getCalculateData(formulaMap.get("RE266"), paramMap);
        paramMap.put("E266", E266);
        //E267
        double E267 = expressionService.getCalculateData(formulaMap.get("RE267"), paramMap);
        paramMap.put("E267", E267);
        //E268
        double E268 = expressionService.getCalculateData(formulaMap.get("RE268"), paramMap);
        paramMap.put("E268", E268);
        //E269
        double E269 = expressionService.getCalculateData(formulaMap.get("RE269"), paramMap);
        paramMap.put("E269", E269);
        //E270
        double E270 = expressionService.getCalculateData(formulaMap.get("RE270"), paramMap);
        paramMap.put("E270", E270);
        //E271
        double E271 = expressionService.getCalculateData(formulaMap.get("RE271"), paramMap);
        paramMap.put("E271", E271);
        //E272
        double E272 = expressionService.getCalculateData(formulaMap.get("RE272"), paramMap);
        paramMap.put("E272", E272);
        //E273
        double E273 = expressionService.getCalculateData(formulaMap.get("RE273"), paramMap);
        paramMap.put("E273", E273);
        //E274
        double E274 = expressionService.getCalculateData(formulaMap.get("RE274"), paramMap);
        paramMap.put("E274", E274);
        //E275
        double E275 = expressionService.getCalculateData(formulaMap.get("RE275"), paramMap);
        paramMap.put("E275", E275);
        //E276
        double E276 = expressionService.getCalculateData(formulaMap.get("RE276"), paramMap);
        paramMap.put("E276", E276);
        //E277
        double E277 = expressionService.getCalculateData(formulaMap.get("RE277"), paramMap);
        paramMap.put("E277", E277);
        //E278
        double E278 = expressionService.getCalculateData(formulaMap.get("RE278"), paramMap);
        paramMap.put("E278", E278);
        //E279
        double E279 = expressionService.getCalculateData(formulaMap.get("RE279"), paramMap);
        paramMap.put("E279", E279);
        //E280
        double E280 = expressionService.getCalculateData(formulaMap.get("RE280"), paramMap);
        paramMap.put("E280", E280);
        //E281
        double E281 = expressionService.getCalculateData(formulaMap.get("RE281"), paramMap);
        paramMap.put("E281", E281);
        //E282
        double E282 = expressionService.getCalculateData(formulaMap.get("RE282"), paramMap);
        paramMap.put("E282", E282);
        //E283
        double E283 = expressionService.getCalculateData(formulaMap.get("RE283"), paramMap);
        paramMap.put("E283", E283);
        //E284
        double E284 = expressionService.getCalculateData(formulaMap.get("RE284"), paramMap);
        paramMap.put("E284", E284);
        //E285
        double E285 = expressionService.getCalculateData(formulaMap.get("RE285"), paramMap);
        paramMap.put("E285", E285);
        //E286
        double E286 = expressionService.getCalculateData(formulaMap.get("RE286"), paramMap);
        paramMap.put("E286", E286);
        //E287
        double E287 = expressionService.getCalculateData(formulaMap.get("RE287"), paramMap);
        paramMap.put("E287", E287);
        //E288
        double E288 = expressionService.getCalculateData(formulaMap.get("RE288"), paramMap);
        paramMap.put("E288", E288);
        //E291
        double E291 = expressionService.getCalculateData(formulaMap.get("RE291"), paramMap);
        paramMap.put("E291", E291);
        //E292
        double E292 = expressionService.getCalculateData(formulaMap.get("RE292"), paramMap);
        paramMap.put("E292", E292);
        //E293
        double E293 = expressionService.getCalculateData(formulaMap.get("RE293"), paramMap);
        paramMap.put("E293", E293);
        //E294
        double E294 = expressionService.getCalculateData(formulaMap.get("RE294"), paramMap);
        paramMap.put("E294", E294);
        //295
        Double RE295_1 = expressionService.getCalculateData(formulaMap.get("RE295_1"), paramMap);
        if (null == RE295_1)
            return null;
        Double RE295_2 = expressionService.getCalculateData(formulaMap.get("RE295_2"), paramMap);
        if (null == RE295_2)
            return null;
        Double RE295_3 = expressionService.getCalculateData(formulaMap.get("RE295_3"), paramMap);
        if (null == RE295_3)
            return null;
        paramMap.put("RET1295", RE295_1 < 1.9 ? RE295_2 : RE295_3);
        double E295 = expressionService.getCalculateData(formulaMap.get("RE295_4"), paramMap);
        paramMap.put("E295", E295);
        //E296
        double E296 = expressionService.getCalculateData(formulaMap.get("RE296"), paramMap);
        paramMap.put("E296", E296);
        //E298
        double E298 = expressionService.getCalculateData(formulaMap.get("RE298"), paramMap);
        paramMap.put("E298", E298);
        //E299
        double E299 = expressionService.getCalculateData(formulaMap.get("RE299"), paramMap);
        paramMap.put("E299", E299);
        Double RE300_1 = expressionService.getCalculateData(formulaMap.get("RE300_1"), paramMap);
        Double RE300_2 = expressionService.getCalculateData(formulaMap.get("RE300_2"), paramMap);
        Double RE300_3 = expressionService.getCalculateData(formulaMap.get("RE300_3"), paramMap);
        paramMap.put("RET1300", RE300_1 < 1.9 ? RE300_2 : RE300_3);
        paramMap.put("E300", expressionService.getCalculateData(formulaMap.get("RE300_4"), paramMap));
        //E301
        double E301 = expressionService.getCalculateData(formulaMap.get("RE301"), paramMap);
        paramMap.put("E301", E301);
        //E303
        double E303 = expressionService.getCalculateData(formulaMap.get("RE303"), paramMap);
        paramMap.put("E303", E303);
        //E310
        double E310 = expressionService.getCalculateData(formulaMap.get("RE310"), paramMap);
        paramMap.put("E310", E310);
        //E311
        double E311 = expressionService.getCalculateData(formulaMap.get("RE311"), paramMap);
        paramMap.put("E311", E311);
        //E312
        double E312 = expressionService.getCalculateData(formulaMap.get("RE312"), paramMap);
        paramMap.put("E312", E312);
        //E313
        double E313 = expressionService.getCalculateData(formulaMap.get("RE313"), paramMap);
        paramMap.put("E313", E313);
        //E314
        double E314 = expressionService.getCalculateData(formulaMap.get("RE314"), paramMap);
        paramMap.put("E314", E314);
        //E315
        double E315 = expressionService.getCalculateData(formulaMap.get("RE315"), paramMap);
        paramMap.put("E315", E315);
        paramMap.put("E331", GE129 < 45 ? 1.0 : 2.0);
        return paramMap;
    }
}
